﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.UI.WebControls;
using Controllerss;
using Core.Contract.DataContract;
using www.Controllers;
//using www.SService;
using www.Controllerss;
using www.Core.Contract.DataContract;

namespace www.Admin
{
    public partial class PostVideo : System.Web.UI.Page
    {
        #region Value

        private readonly ObjectCache _cache = MemoryCache.Default;
        static bool _insert = false;
        private static List<RelaxVideoDataContract> _lAdvertiseValue;
        //private readonly HyServicesClient _service = new HyServicesClient();
        private readonly LanguageController _languageController = new LanguageController();
        private readonly StaffController _staffController = new StaffController();
        private readonly MenuMemberController _menuMemberController = new MenuMemberController();
        private readonly MenuController _menuController = new MenuController();
        private readonly CategoryController _categoryController = new CategoryController();
        private readonly VideoController _videoController = new VideoController();
        static int _id = 0;
        private static Boolean _insertNew = false;
        private XMLClass _xmlClass=new XMLClass();

        #endregion

        #region Contructor

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Cache["Language"] == null)
            {
                var lLanguage = _languageController.GetLanguageDataContracts();
                if (lLanguage.Any())
                    Cache.Insert("Language", lLanguage.ToList(), null, DateTime.MaxValue, TimeSpan.FromDays(30));
            }
            if (_cache["Employee"] == null)
            {
                var lData = _staffController.GetStaffDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Employee", lData.ToList(), policy);
                }
            }

            if (!IsPostBack)
            {
                var lLanguages = (List<RelaxLanguageDataContract>)Cache["Language"];
                if (lLanguages != null)
                {
                    var lReturn = lLanguages.Where(l => l.Id != 1 && l.Type == 1);
                    Addlist(lReturn);
                }
                LoadCategory();
                LoadData();
            }
        }
        #endregion

        #region Event
        protected void ddlgroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void lbtinsert_Click(object sender, EventArgs e)
        {
            pnviews.Visible = false;
            pncontrols.Visible = true;
            Donew();
            _insert = true;
            _id = 0;
            _lAdvertiseValue = null;
        }

        protected void lbtback_Click(object sender, EventArgs e)
        {
            try
            {
                Back();
            }
            catch (Exception)
            {

            }
        }

        protected void lbtrefresh_Click(object sender, EventArgs e)
        {
            try
            {
                LoadData();
            }
            catch (Exception)
            {

            }

        }

        protected void lbtdel_Click(object sender, EventArgs e)
        {
            try
            {
                Delete();
            }
            catch (Exception)
            {

            }

        }

        protected void grid_advertise_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grid_PostConfide.PageIndex = e.NewPageIndex;
                LoadData();
            }
            catch (Exception)
            {

            }
        }

        protected void grid_advertise_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                _id = Convert.ToInt32(e.CommandArgument);
                var lData = (List<RelaxVideoDataContract>)_cache["Video"];
                if (lData == null)
                    return;
                var first = lData.FirstOrDefault(p => p.Id == _id && p.Languagecode == 1);
                if (first == null)
                    return;
                if (e.CommandName == "cmd_edit")
                {
                    txtname.Value = first.Name;
                    txtUrl.Value = @"https://www.youtube.com/watch?v=" + first.Code;
                    txtord.Value = first.Priority.ToString();
                    chkactive.Checked = first.Active == 1;
                    ddlCategory.Value = first.CategoryId.ToString();
                    var lValue = lData.Where(p => p.Id == _id && p.Languagecode != 1).ToList();
                    _lAdvertiseValue = lValue;
                    var lReturn = from c in lValue
                                  select new { c.Active, c.Id, c.Languagecode, c.Name, c.Priority, c.Tag, c.Code };

                    grvNgonngu.DataSource = lReturn.ToList();
                    grvNgonngu.DataBind();

                    pncontrols.Visible = true;
                    pnviews.Visible = false;
                    _insert = false;

                }
                else if (e.CommandName == "cmd_delete")
                {

                    DeleteLanguage(1);
                    lData.Remove(first);
                    _cache["Video"] = lData;
                    LoadData();
                    Message("Xóa Thành Công");
                    _id = 0;
                    _lAdvertiseValue = null;

                }
                else if (e.CommandName == "cmd_active")
                {
                    first.Active = first.Active == 1 ? 0 : 1;

                    _videoController.Update(new List<RelaxVideoDataContract> { first });
                    _cache["Video"] = lData;
                    LoadData();
                    Message("Cập nhật Thành Công");

                    _id = 0;
                    _lAdvertiseValue = null;
                }
                else if (e.CommandName == "cmd_Detail")
                {
                    frmDetail.DataSource = new List<RelaxVideoDataContract> { first };
                    frmDetail.DataBind();
                    pnDetail.Visible = true;
                    pnviews.Visible = false;
                    pncontrols.Visible = false;
                }

            }
            catch (Exception)
            {

            }
        }

        protected void grvNgonngu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                var languageCode = e.CommandArgument.ToString();

                if (e.CommandName == "cmd_edit")
                {
                    EditLanguage(Convert.ToInt32(languageCode));
                    _insertNew = true;
                }
                else if (e.CommandName == "cmd_delete")
                {
                    DeleteLanguage(Convert.ToInt32(languageCode));
                }
            }
            catch (Exception)
            {

            }
        }

        protected void grid_PostConfide_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var userId = ((Label)e.Row.FindControl("lblUserId")).Text;
                if (!string.IsNullOrEmpty(userId))
                {
                    var userName = (Label)e.Row.FindControl("lblUserName");
                    var lData = (List<RelaxStaffDataContract>)_cache["Employee"];
                    if (lData != null)
                    {
                        var first = lData.FirstOrDefault(p => p.LoginId == Convert.ToInt32(userId));
                        if (first != null)
                        {
                            userName.Text = first.Name;
                        }
                    }
                }
            }
        }

        protected void lbtsave_Click(object sender, EventArgs e)
        {
            try
            {
                Save();
            }
            catch (Exception)
            {

            }
        }

        protected void lbtdonew_Click(object sender, EventArgs e)
        {
            try
            {
                Donew();
            }
            catch (Exception)
            {

            }

        }

        protected void btnThemValue_Click(object sender, EventArgs e)
        {
            try
            {
                var lData = (List<RelaxVideoDataContract>)_cache["News"];
                InsertLanguageCode(lData, _id);
                txtName_Value.Value = txtUrlValue.Value = string.Empty;
                _insertNew = false;
            }
            catch (Exception)
            {

            }
        }
        #endregion

        #region Business
        private void LoadStaffMenu()
        {
            if (_cache["StaffMenu"] == null)
            {
                var lData = _menuMemberController.GetMenuMemberDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("StaffMenu", lData.ToList(), policy);
                }
            }
            if (_cache["Menu"] == null)
            {
                var lData = _menuController.GetMenuDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Menu", lData.ToList(), policy);
                }
            }
            if (_cache["Category"] == null)
            {
                var lData = _categoryController.GetCategoryDataContracts();
                var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                _cache.Set("Category", lData.ToList(), policy);
            }
        }
        private void LoadCategory()
        {
            LoadStaffMenu();
            var lMenuStaff = (List<RelaxMenuMemberDataContract>)_cache["StaffMenu"];
            var lMenu = (List<RelaxMenuDataContract>)_cache["Menu"];
            var lCategory = (List<RelaxCategoryDataContract>)_cache["Category"];
            if (lMenu == null)
                return;
            if (lMenuStaff == null)
                return;
            if (lCategory == null)
                return;
            var lReturn = (from c in lCategory.Where(p => p.Languagecode == 1 && p.Active == Constant.Active.Display)
                           join m in lMenu.Where(p => p.Languagecode == 1 && p.Active == Constant.Active.Display) on c.Id equals m.CategoryId
                           join s in lMenuStaff.Where(p => p.MemberId == Convert.ToInt32(Session["StaffId"]) && p.Active == Constant.Active.Display) on m.Id equals
                    s.MenuId
                           select new { c.Id, c.Name, c.Type }).ToList();
            var tmp = lReturn.ToList();
            foreach (var item in tmp)
            {
                var checkItem = lReturn.Where(p => p.Id == item.Id).ToList();
                if (checkItem.Count > 1)
                {
                    var first = checkItem.FirstOrDefault();
                    foreach (var item1 in checkItem)
                    {
                        lReturn.Remove(item1);
                    }
                    lReturn.Add(first);
                }
            }
            ddlCategory1.Items.Add(new ListItem("---Chọn---", "0"));
            ddlCategory.Items.Add(new ListItem("---Chọn---", "0"));
            var lParent = lReturn.Where(p => p.Type == 0).ToList();
            if (lParent.Any())
            {
                foreach (var item in lParent)
                {
                    ddlCategory1.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                    ddlCategory.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                    var lreturn1 = lCategory.Where(p => p.Type == item.Id && p.Languagecode == 1).ToList();
                    if (lreturn1.Any())
                    {
                        foreach (var item1 in lreturn1)
                        {
                            ddlCategory1.Items.Add(new ListItem("--" + item1.Name, item1.Id.ToString()));
                            ddlCategory.Items.Add(new ListItem("--" + item1.Name, item1.Id.ToString()));
                        }
                    }
                }
            }
            else
            {
                foreach (var item in lReturn)
                {
                    ddlCategory1.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                    ddlCategory.Items.Add(new ListItem(item.Name, item.Id.ToString()));
                }
            }

        }

        private void Addlist(IEnumerable<RelaxLanguageDataContract> lLanguageDataContracts)
        {
            ddlLanguage.Items.Clear();
            ddlLanguage.Items.Add(new ListItem("---Chọn---", "0"));
            foreach (var item in lLanguageDataContracts)
            {
                var items = new ListItem(item.Name, item.Id.ToString());
                ddlLanguage.Items.Add(items);
            }
        }
        private void InsertLanguageCode(IEnumerable<RelaxVideoDataContract> lData, int iD = 0)
        {
            if (!string.IsNullOrEmpty(ddlLanguage.Value))
            {
                if (string.IsNullOrEmpty(txtName_Value.Value) && string.IsNullOrEmpty(txtUrlValue.Value))
                {
                    Message("Bạn phải nhập đầy đủ các gợi ý trên");
                    return;
                }
                if (!txtUrlValue.Value.Contains("www.youtube.com"))
                {
                    Message("Bạn phải nhập url video của youtue");
                    return;
                }
                var code = txtUrlValue.Value.IndexOf('?') > 0
                    ? txtUrlValue.Value.Substring(txtUrlValue.Value.IndexOf("v=") + 2)
                    : null;
                if (string.IsNullOrEmpty(code))
                {
                    Message("Bạn nhập sai địa chỉ url. địa chỉ có dạng:https://www.youtube.com/watch?v=Ztk_oZdsjpc hoặc https://www.youtube.com/watch?v=Ztk_oZdsjpc&spfreload=10 ");
                    return;
                }
                var ytCode = code.IndexOf('&') > 0 ? code.Substring(0, code.Length - (code.IndexOf('&') + 2)) : code;
                if (iD > 0)
                {
                    if (_lAdvertiseValue == null)
                    {
                        if (lData != null)
                        {
                            var lVaue =
                                lData.Where(p => p.Id == iD && p.Languagecode != 1).ToList();
                            if (!lVaue.Any())
                                lVaue = new List<RelaxVideoDataContract>();
                            _lAdvertiseValue = lVaue;
                        }
                        else
                            _lAdvertiseValue = new List<RelaxVideoDataContract>();
                    }

                    var languageCode = Convert.ToInt32(ddlLanguage.Value);
                    var check = _lAdvertiseValue.Where(p => p.Languagecode == languageCode).ToList();

                    if (check.Any())
                    {
                        if (_insertNew)
                        {
                            var firstordefault = check.FirstOrDefault();
                            if (firstordefault != null)
                            {
                                firstordefault.Languagecode = languageCode;
                                firstordefault.Name = txtName_Value.Value.Replace("'", "");
                                firstordefault.Tag = txtName_Value.Value.Replace("'", "").Replace(' ', '-');
                                firstordefault.Code = ytCode;
                            }
                        }
                        else
                        {
                            Message("Ngôn ngữ này đã tồn tại.");
                            return;
                        }
                    }
                    else
                    {
                        var advertise = new RelaxVideoDataContract
                        {
                            Id = _id,
                            Languagecode = languageCode,
                            Name = txtName_Value.Value.Replace("'", ""),
                            Tag = txtName_Value.Value.Replace("'", "").Replace(' ', '-'),
                            Code = ytCode,
                            Report = 0
                        };
                        _lAdvertiseValue.Add(advertise);
                    }
                }
                else
                {
                    if (_lAdvertiseValue == null)
                        _lAdvertiseValue = new List<RelaxVideoDataContract>();

                    var languageCode = Convert.ToInt32(ddlLanguage.Value);
                    var check = _lAdvertiseValue.Where(p => p.Languagecode == languageCode).ToList();
                    if (check.Any())
                    {
                        if (_insertNew)
                        {
                            var firstordefault = check.FirstOrDefault();
                            if (firstordefault != null)
                            {
                                firstordefault.Languagecode = languageCode;
                                firstordefault.Name = txtName_Value.Value.Replace("'", "");
                                firstordefault.Tag = txtName_Value.Value.Replace("'", "").Replace(' ', '-');
                                firstordefault.Code = ytCode;
                            }
                        }
                        else
                        {
                            Message("Ngôn ngữ này đã tồn tại.");
                            return;
                        }
                    }
                    else
                    {
                        var advertise = new RelaxVideoDataContract
                        {
                            Languagecode = languageCode,
                            Name = txtName_Value.Value.Replace("'", ""),
                            Tag = txtName_Value.Value.Replace("'", "").Replace(' ', '-'),
                            Code = ytCode,
                            Report = 0
                        };
                        _lAdvertiseValue.Add(advertise);
                    }

                }
                grvNgonngu.DataSource = _lAdvertiseValue;
                grvNgonngu.DataBind();

            }
            else
            {
                Message("Bạn phải chọn ngôn ngữ.");
            }
        }
       
        private void EditLanguage(int iLanguageCode)
        {
            if (iLanguageCode <= 0) return;
            var first = _lAdvertiseValue.FirstOrDefault(p => p.Languagecode == iLanguageCode);
            if (first != null)
            {
                txtName_Value.Value = first.Name;
                txtUrlValue.Value = @"https://www.youtube.com/watch?v=" + first.Code;
                ddlLanguage.Value = first.Languagecode.ToString();

            }
        }
        private void DeleteLanguage(int iLanguageCode)
        {
            if (iLanguageCode <= 0) return;

            if (_id > 0)
            {
                if (_videoController.Delete(new List<long> { _id }, iLanguageCode))
                {

                }
                if (_lAdvertiseValue != null)
                {
                    var first = _lAdvertiseValue.FirstOrDefault(p => p.Languagecode == iLanguageCode);
                    _lAdvertiseValue.Remove(first);
                }
            }
            else
            {
                if (_lAdvertiseValue != null)
                {
                    var first = _lAdvertiseValue.FirstOrDefault(p => p.Languagecode == iLanguageCode);
                    _lAdvertiseValue.Remove(first);
                }
            }
            grvNgonngu.DataSource = _lAdvertiseValue;
            grvNgonngu.DataBind();
        }
        private void Save()
        {
            try
            {
                if (string.IsNullOrEmpty(txtname.Value) ||
                    string.IsNullOrEmpty(txtord.Value) || string.IsNullOrEmpty(ddlCategory.Value) || string.IsNullOrEmpty(txtUrl.Value))
                {
                    Message("Bạn phải nhập đầy đủ thông tin gợi ý trên");
                    return;
                }
                Int32 ord;
                if (!string.IsNullOrEmpty(txtord.Value) && !Int32.TryParse(txtord.Value, out ord))
                {
                    Message("Thứ tự phải là số.");
                    return;
                }
                if (Convert.ToInt32(ddlCategory.Value) <= 0)
                {
                    Message("Bạn phải chọn Danh mục.");
                    return;
                }
                if (!txtUrl.Value.Contains("www.youtube.com"))
                {
                    Message("Bạn phải nhập url video của youtue.");
                    return;
                }
                var code = txtUrl.Value.IndexOf('?') > 0
                  ? txtUrl.Value.Substring(txtUrl.Value.IndexOf("v=") + 2)
                  : null;
                if (string.IsNullOrEmpty(code))
                {
                    Message("Bạn nhập sai địa chỉ url. địa chỉ có dạng:https://www.youtube.com/watch?v=Ztk_oZdsjpc hoặc https://www.youtube.com/watch?v=Ztk_oZdsjpc&spfreload=10 ");
                    return;
                }
                var ytCode = code.IndexOf('&') > 0 ? code.Substring(0, code.Length - (code.IndexOf('&') + 2)) : code;

                var lData = (List<RelaxVideoDataContract>)_cache["Video"];

                var ac = chkactive.Checked ? 1 : 0;
                
                if (!_insert)
                {
                    var lUpdate = new List<RelaxVideoDataContract>();
                    if (lData == null)
                        return;
                    var first =
                        lData.FirstOrDefault(
                            p => p.Id == _id && p.Languagecode == 1);
                    if (first == null)
                        return;

                    var advertise = new RelaxVideoDataContract
                    {
                        Id = _id,
                        Active = ac,
                        Priority = string.IsNullOrEmpty(txtord.Value) ? 0 : Convert.ToInt32(txtord.Value),
                        Name = txtname.Value.Replace("'", ""),
                        Tag = txtname.Value.Replace("'", "").Replace(' ', '-'),
                        Code = ytCode,
                        Languagecode = 1,
                        IsDefault = 0,
                        CategoryId = Convert.ToInt32(ddlCategory.Value),
                        Userid = Convert.ToInt32(Session["StaffId"]),
                        Date = first.Date,
                        Report = first.Report,
                        UserBrower = first.UserBrower,
                        View = first.View
                    };
                    lUpdate.Add(advertise);

                    if (_lAdvertiseValue != null && _lAdvertiseValue.Count > 0)
                    {
                        lUpdate.AddRange(_lAdvertiseValue);
                    }
                    if (_videoController.Update(lUpdate))
                    {
                        var url = string.Empty;
                        foreach (var item in lUpdate)
                        {
                            var firstordefault =
                                lData.FirstOrDefault(p => p.Id == item.Id && p.Languagecode == item.Languagecode);
                            if (firstordefault != null)
                            {
                                firstordefault.Name = item.Name;
                                firstordefault.Tag = item.Tag;
                                firstordefault.Code = item.Code;
                                firstordefault.Priority = lUpdate[0].Priority;
                                firstordefault.Active = lUpdate[0].Active;
                                firstordefault.Userid = lUpdate[0].Userid;
                                firstordefault.IsDefault = lUpdate[0].IsDefault;
                                firstordefault.CategoryId = lUpdate[0].CategoryId;
                                firstordefault.View = lUpdate[0].View;
                                firstordefault.Date = lUpdate[0].Date;
                                firstordefault.UserBrower = lUpdate[0].UserBrower;
                                firstordefault.Report = item.Report;
                            }
                            else
                            {
                                item.Priority = lUpdate[0].Priority;
                                item.Active = lUpdate[0].Active;
                                item.Userid = lUpdate[0].Userid;
                                item.IsDefault = lUpdate[0].IsDefault;
                                item.CategoryId = lUpdate[0].CategoryId;
                                item.View = lUpdate[0].View;
                                item.Date = lUpdate[0].Date;
                                item.UserBrower = lUpdate[0].UserBrower;
                                lData.Add(item);
                            }
                            url = "http://relax2s.com/Home/VideoDetail/" + (item.Languagecode == 1 ? "vi-VN" : item.Languagecode == 2 ? "en-US" : "zh-CN") + "/" + item.Id + "/" + item.Tag;
                            lblMessageLink.InnerHtml += "<div>" + url + "</div>";
                            //_xmlClass.WriteUrl();
                            
                        }

                        _cache["Video"] = lData;
                        Message("Sửa Thành Công !");
                        grvNgonngu.DataSource = null;
                        grvNgonngu.DataBind();
                        LoadData();
                        _id = 0;
                        _lAdvertiseValue = null;
                        pncontrols.Visible = false;
                        pnviews.Visible = true;
                    }
                }
                else
                {

                    var lAdd = new List<RelaxVideoDataContract>();
                    var advertise = new RelaxVideoDataContract
                    {
                        Active = ac,
                        Name = txtname.Value.Replace("'", ""),
                        Tag = txtname.Value.Replace("'", "").Replace(' ', '-'),
                        Code = ytCode,
                        Priority = string.IsNullOrEmpty(txtord.Value) ? 0 : Convert.ToInt32(txtord.Value),
                        Languagecode = 1,
                        IsDefault = 0,
                        View = 0,
                        Date = DateTime.Now,
                        CategoryId = Convert.ToInt32(ddlCategory.Value),
                        Userid = Convert.ToInt32(Session["StaffId"]),
                        Report = 0,
                        UserBrower = 0
                    };

                    lAdd.Add(advertise);
                    if (_lAdvertiseValue != null && _lAdvertiseValue.Count > 0)
                    {
                        lAdd.AddRange(_lAdvertiseValue);
                    }

                    var lId = _videoController.Add(lAdd);
                    var url = string.Empty;
                    foreach (var item in lAdd)
                    {
                        item.Priority = lAdd[0].Priority;
                        item.Active = lAdd[0].Active;
                        item.Userid = lAdd[0].Userid;
                        item.IsDefault = lAdd[0].IsDefault;
                        item.CategoryId = lAdd[0].CategoryId;
                        item.Id = lId;
                        item.View = lAdd[0].View;
                        item.Date = lAdd[0].Date;
                        item.UserBrower = lAdd[0].UserBrower;
                        url = "http://relax2s.com/Home/VideoDetail/" + (item.Languagecode == 1 ? "vi-VN" : item.Languagecode == 2 ? "en-US" : "zh-CN") + "/" + item.Id + "/" + item.Tag;
                        lblMessageLink.InnerHtml += "<div>" + url + "</div>";
                        //_xmlClass.WriteUrl();
                    }

                    if (lData == null)
                        lData = new List<RelaxVideoDataContract>();
                    lData.AddRange(lAdd);
                    _cache["Video"] = lData;
                    grvNgonngu.DataSource = null;
                    grvNgonngu.DataBind();
                    Message("Thêm Thành Công !");
                }

            }
            catch (Exception ex)
            {
                Message(ex.Message);
            }
        }
        private void Back()
        {
            pnviews.Visible = true;
            pncontrols.Visible = false;
            pnDetail.Visible = false;
            _insert = false;
            _id = 0;
            _lAdvertiseValue = null;
            _insertNew = false;
            LoadData();
        }

        private void Delete()
        {
            try
            {
                if (grid_PostConfide.Rows.Count <= 0) return;
                var hang = 0;
                var lAd = new List<long>();
                for (var i = 0; i < grid_PostConfide.Rows.Count; ++i)
                {
                    var chkselect = ((CheckBox)grid_PostConfide.Rows[i].FindControl("chkselect")).Checked;
                    if (chkselect)
                    {
                        var id = ((Label)grid_PostConfide.Rows[i].FindControl("lblId")).Text;
                        lAd.Add(Convert.ToInt32(id));
                    }
                }
                if (_videoController.Delete(lAd, 1))
                {
                    var lData = (List<RelaxVideoDataContract>)_cache["Video"];
                    if ((lData.Any()))
                    {
                        foreach (var id in lAd)
                        {
                            var first = lData.FirstOrDefault(p => p.Id == id && p.Languagecode == 1);
                            if (first != null)
                                lData.Remove(first);
                            hang++;
                        }
                    }
                    if (hang != 0)
                    {
                        Message("Có " + hang + "bản ghi được Xóa Thành Công");
                    }
                    else
                    {
                        Message("Không có bản ghi nào được xóa !");

                    }
                    _cache["Video"] = lData;
                    LoadData();
                }

            }
            catch { }
        }
        public void LoadData()
        {
            grid_PostConfide.DataSourceID = null;
            if (_cache["Video"] == null)
            {
                var lData = _videoController.GeVideoDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Video", lData.ToList(), policy);
                }
            }
            var lData1 = (List<RelaxVideoDataContract>)_cache["Video"];
            if (lData1 != null)
            {
                var lReturn = from c in lData1.Where(p => p.Languagecode == 1 && p.Userid == Convert.ToInt32(Session["StaffId"]) && p.CategoryId == Convert.ToInt32(string.IsNullOrEmpty(ddlCategory1.SelectedValue) ? "0" : ddlCategory1.SelectedValue))
                              select new { c.Active, c.Id, c.Languagecode, c.Name, c.Priority, c.Tag, c.Date, c.Userid, c.Code };
                grid_PostConfide.DataSource = lReturn.ToList();
            }
            grid_PostConfide.DataBind();
        }

        private void Donew()
        {
            chkactive.Checked = false;
            txtUrl.Value = "";
            txtname.Value = "";
            txtName_Value.Value = "";
            txtord.Value = "";
            txtUrlValue.Value = "";

            txtname.Focus();
        }
        private void Message(string str)
        {
            Response.Write("<script>alert('" + str + "');</script>");
        }
        #endregion
    }
}